/**
 * jQuery EasyUI 1.4
 * 
 * Copyright (c) 2009-2014 www.jeasyui.com. All rights reserved.
 * 
 * Licensed under the GPL license: http://www.gnu.org/licenses/gpl.txt To use it
 * on other terms please contact us at info@jeasyui.com
 * 
 */
(function($) {
	function buildToolbar(target) {
		var pagination = $.data(target, "pagination");
		var opts = pagination.options;
		// 设置bb缓存
		var bb = pagination.bb = {};
		var pageBody=$(target).addClass("pagination");
		var hmtlPage=[];
		if ("before"==opts.buttonPosition && opts.buttons) {
			if($.isFunction(opts.buttons)){
				var jBut=opts.buttons();
				if(jBut instanceof jQuery){
					hmtlPage.push("<div class=\"fl mb8\">");
					hmtlPage.push(opts.buttons().html());
					hmtlPage.push("</div>");
				}else if(typeof jBut == "string"){
					hmtlPage.push("<div class=\"fl mb8\">");
					hmtlPage.push(jBut);
					hmtlPage.push("</div>");
				}
			}else if(typeof jBut == "string"){
				hmtlPage.push("<div class=\"fl mb8\">");
				hmtlPage.push($(opts.buttons));
				hmtlPage.push("</div>");
			}
			//$("<td><div class=\"pagination-btn-separator\"></div></td>").appendTo(tr);
		}
		//需要清空之前的东西不然的，会出现
		pageBody.empty();
		pageBody.append($(hmtlPage.join("")));
		var pagetConext=$("<div class=\"e-expand-page\"></div>");
		
		var pager = opts.message||"";
		if(opts.total && opts.total>0){
			pager = $("<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\"><tr></tr></table>");
			var tr = pager.find("tr");
			var layoutArray = $.extend([], opts.layout);
			if (!opts.showPageList) {
				_inarray(layoutArray, "list");
			}
			if (!opts.showRefresh) {
				_inarray(layoutArray, "refresh");
			}
			if (layoutArray[0] == "sep") {
				layoutArray.shift();
			}
			if (layoutArray[layoutArray.length - 1] == "sep") {
				layoutArray.pop();
			}
			
			for (var i = 0; i < layoutArray.length; i++) {
				var lay = layoutArray[i];
				if (lay == "list") {
					var ps = $("<select class=\"pagination-page-list\"></select>");
					ps.bind("change", function() {
						opts.pageSize = parseInt($(this).val());
						opts.onChangePageSize.call(target, opts.pageSize);
						selectPage(target, opts.pageNumber);
					});
					for (var m = 0; m < opts.pageList.length; m++) {
						$("<option></option>").text(opts.pageList[m]).appendTo(ps);
					}
					$("<td></td>").append(ps).appendTo(tr);
				} else { if (lay == "sep") {
					$("<td><div class=\"pagination-btn-separator\"></div></td>").appendTo(tr);
				} else { if (lay == "first") {
					bb.first = _findNavButtion("first");
				} else { if (lay == "prev") {
					bb.prev = _findNavButtion("prev");
				} else { if (lay == "next") {
					bb.next = _findNavButtion("next");
				} else { if (lay == "last") {
					bb.last = _findNavButtion("last");
				} else { if (lay == "manual") {
					if(opts.showPageStyle=="links"){
						$("<td class=\"pagination-links\"></td>").appendTo(tr);
					}else{
						$("<span style=\"padding-left:6px;\"></span>").html(opts.beforePageText).appendTo(tr).wrap("<td></td>");
						bb.num = $("<input class=\"pagination-num\" type=\"text\" value=\"1\" size=\"2\"><span class=\"_total_pageNum\"></span>").appendTo(tr).wrap("<td></td>");
						bb.num.unbind(".pagination").bind("keydown.pagination",function(e) {
								if (e.keyCode == 13) {
								var _a = parseInt($(this).val()) || 1;
								selectPage(target, _a);
								return false;
							}
						});
						bb.after = $("<span style=\"padding-right:6px;\"></span>").appendTo(tr).wrap("<td></td>");
					}
				} else {if (lay == "refresh") {
					bb.refresh = _findNavButtion("refresh");
				} else { if (lay == "links") {
					if(opts.showPageStyle=="manual"){
						$("<span style=\"padding-left:6px;\"></span>").appendTo(tr).wrap("<td></td>");
						bb.num = $("<input class=\"pagination-num\" type=\"text\" value=\"1\" size=\"2\"><span class=\"_total_pageNum\"></span>").appendTo(tr).wrap("<td></td>");
						bb.num.unbind(".pagination").bind("keydown.pagination",function(e) {
								if (e.keyCode == 13) {
								var _a = parseInt($(this).val()) || 1;
								selectPage(target, _a);
								return false;
							}
						});
						//bb.after = $("<span style=\"padding-right:6px;\"></span>").appendTo(tr).wrap("<td></td>");
					}else{
						$("<td class=\"pagination-links\"></td>").appendTo(tr);
					}
					
				}}}}}}}}}
			}
			if ("after"==opts.buttonPosition && opts.buttons) {
				$("<td><div class=\"pagination-btn-separator\"></div></td>").appendTo(tr);
				if ($.isArray(opts.buttons)) {
					for (var n = 0; n < opts.buttons.length; n++) {
						var _b = opts.buttons[n];
						if (_b == "-") {
							$("<td><div class=\"pagination-btn-separator\"></div></td>").appendTo(tr);
						} else {
							var td = $("<td></td>").appendTo(tr);
							var a = $("<a href=\"javascript:void(0)\"></a>").appendTo(td);
							a[0].onclick = eval(_b.handler || function() { });
							a.linkbutton($.extend({}, _b, {
								plain : true
							}));
						}
					}
				}else if($.isFunction(opts.buttons)){
					opts.buttons().appendTo($("<td></td>").appendTo(tr)).show();
				}else{
					$(opts.buttons).appendTo($("<td></td>").appendTo(tr)).show();
				}
			}
		}
		pagetConext.append(pager);
		pagetConext.append($("<div class=\"pagination-info\"></div>"));
		pagetConext.append($("<div style=\"clear:both;\"></div>"));
		pageBody.append(pagetConext);
//		$("<div class=\"pagination-info\"></div>").appendTo(pager);
//		$("<div style=\"clear:both;\"></div>").appendTo(pager);
		function _findNavButtion(name) {
			var nav = opts.nav[name];
			var a = $("<a href=\"javascript:void(0)\"></a>").appendTo(tr);
			a.wrap("<td></td>");
			a.linkbutton({
				iconCls : nav.iconCls,
				plain : true
			}).unbind(".pagination").bind("click.pagination", function() {
				nav.handler.call(target);
			});
			return a;
		};
		function _inarray(array, value) {
			var _f = $.inArray(value, array);
			if (_f >= 0) {
				array.splice(_f, 1);
			}
			return array;
		};
	};
	function selectPage(target, page) {
		var opts = $.data(target, "pagination").options;
		refresh(target, {
			pageNumber : page
		});
		opts.onSelectPage.call(target, opts.pageNumber, opts.pageSize);
	};
	function refresh(target, param) {
		var pagination = $.data(target, "pagination");
		var opts = pagination.options;
		var bb = pagination.bb;
		$.extend(opts, param || {});
		if(opts.total<=0){
			return;
		}
		var ps = $(target).find("select.pagination-page-list");
		if (opts.showPageList && ps.length) {
			ps.val(opts.pageSize + "");
			opts.pageSize = parseInt(ps.val());
		}
		var pageNum = Math.ceil(opts.total / opts.pageSize) || 1;
		if (opts.pageNumber < 1) {
			opts.pageNumber = 1;
		}
		if (opts.pageNumber > pageNum) {
			opts.pageNumber = pageNum;
		}
		if (opts.total == 0) {
			opts.pageNumber = 0;
			pageNum = 0;
		}
		if (bb.num) {
			bb.num.val(opts.pageNumber);
		}
		
		var tdPageNum = $(target).find("span._total_pageNum");
		if(tdPageNum && tdPageNum.length>0){
			tdPageNum.html("/"+pageNum+"\u9875");
		}
		
		if (bb.after) {
			bb.after.html(opts.afterPageText.replace(/{pages}/, pageNum));
		}
		var td = $(target).find("td.pagination-links");
		if (td.length) {
			td.empty();
			var meanNum = opts.pageNumber - Math.floor(opts.links / 2);
			if (meanNum < 1) {
				meanNum = 1;
			}
			var _1b = meanNum + opts.links - 1;
			if (_1b > pageNum) {
				_1b = pageNum;
			}
			meanNum = _1b - opts.links + 1;
			if (meanNum < 1) {
				meanNum = 1;
			}
			for (var i = meanNum; i <= _1b; i++) {
				var a = $("<a class=\"pagination-link\" href=\"javascript:void(0)\"></a>").appendTo(td);
				a.linkbutton({
					plain : true,
					text : i
				});
				if (i == opts.pageNumber) {
					a.linkbutton("select");
				} else {
					a.unbind(".pagination").bind("click.pagination", {
						pageNumber : i
					}, function(e) {
						selectPage(target, e.data.pageNumber);
					});
				}
			}
		}
		if(opts.showMsg){
			var message = opts.displayMsg;
			message = message.replace(/{from}/, opts.total == 0 ? 0 : opts.pageSize * (opts.pageNumber - 1) + 1);
			message = message.replace(/{to}/, Math.min(opts.pageSize * (opts.pageNumber), opts.total));
			message = message.replace(/{total}/, opts.total);
			$(target).find("div.pagination-info").html(message);
		}
		if (bb.first) {
			bb.first.linkbutton({
				disabled : ((!opts.total) || opts.pageNumber == 1)
			});
		}
		if (bb.prev) {
			bb.prev.linkbutton({
				disabled : ((!opts.total) || opts.pageNumber == 1)
			});
		}
		if (bb.next) {
			bb.next.linkbutton({
				disabled : (opts.pageNumber == pageNum)
			});
		}
		if (bb.last) {
			bb.last.linkbutton({
				disabled : (opts.pageNumber == pageNum)
			});
		}
		setLoadStatus(target, opts.loading);
	};
	function setLoadStatus(target, loading) {
		var pagination = $.data(target, "pagination");
		var opts = pagination.options;
		opts.loading = loading;
		if (opts.showRefresh && pagination.bb.refresh) {
			pagination.bb.refresh.linkbutton({
				iconCls : (opts.loading ? "pagination-loading" : "pagination-load")
			});
		}
	};
	
	function reload(target){
		var pagination = $.data(target, "pagination");
		var opts = pagination.options;
		if (opts.onBeforeRefresh.call(target, opts.pageNumber, opts.pageSize) != false) {
			$(target).pagination("select", opts.pageNumber);
			opts.onRefresh.call(target, opts.pageNumber, opts.pageSize);
		}
	};
	
	$.fn.pagination = function(options, param) {
		if (typeof options == "string") {
			return $.fn.pagination.methods[options](this, param);
		}
		options = options || {};
		return this.each(function() {
			var opts;
			var pagination = $.data(this, "pagination");
			if (pagination) {
				opts = $.extend(pagination.options, options);
			} else {
				opts = $.extend({}, $.fn.pagination.defaults, $.fn.pagination.parseOptions(this), options);
				$.data(this, "pagination", {
					options : opts
				});
			}
			buildToolbar(this);
			refresh(this);
		});
	};
	$.fn.pagination.methods = {
		options : function(jq) {
			return $.data(jq[0], "pagination").options;
		},
		loading : function(jq) {
			return jq.each(function() {
				setLoadStatus(this, true);
			});
		},
		loaded : function(jq) {
			return jq.each(function() {
				setLoadStatus(this, false);
			});
		},
		refresh : function(jq, param) {
			return jq.each(function() {
				refresh(this, param);
			});
		},
		reload : function(jq, param) {
			return jq.each(function() {
				reload(this, param);
			});
		},
		select : function(jq, param) {
			return jq.each(function() {
				selectPage(this, param);
			});
		}
	};
	$.fn.pagination.parseOptions = function(target) {
		var t = $(target);
		return $.extend({}, $.parser.parseOptions(target, [ {
			total : "number",
			pageSize : "number",
			pageNumber : "number",
			links : "number"
		}, {
			loading : "boolean",
			showPageList : "boolean",
			showRefresh : "boolean"
		} ]), {
			pageList : (t.attr("pageList") ? eval(t.attr("pageList"))
					: undefined)
		});
	};
	$.fn.pagination.defaults = {
		total : 1,
		pageSize : 10,
		//before
		buttonPosition : "after",
		pageNumber : 1,
		pageList : [ 10, 20, 30, 50 ],
		loading : false,
		buttons : null,
		showPageList : true,
		showRefresh : true,
		showMsg:true,
		links : 10,
		showPageStyle:"links",
		layout : [ "list", "first", "prev", "links", "next", "last", "refresh" ],
		onSelectPage : function(pageNumber, pageSize) {
		},
		onBeforeRefresh : function(pageNumber, pageSize) {
		},
		onRefresh : function(pageNumber, pageSize) {
		},
		onChangePageSize : function(pageSize) {
		},
		message : "",
		beforePageText : "\u9875",
		afterPageText : "\u9875\u7801:{pages}",
		displayMsg : "\u663E\u793A {from} \u5230 {to} \u603B\u6570:{total} ",
		nav : {
			first : {
				iconCls : "pagination-first",
				handler : function() {
					var opts = $(this).pagination("options");
					if (opts.pageNumber > 1) {
						$(this).pagination("select", 1);
					}
				}
			},
			prev : {
				iconCls : "pagination-prev",
				handler : function() {
					var opts = $(this).pagination("options");
					if (opts.pageNumber > 1) {
						$(this).pagination("select", opts.pageNumber - 1);
					}
				}
			},
			next : {
				iconCls : "pagination-next",
				handler : function() {
					var opts = $(this).pagination("options");
					var pageNumber = Math.ceil(opts.total / opts.pageSize);
					if (opts.pageNumber < pageNumber) {
						$(this).pagination("select", opts.pageNumber + 1);
					}
				}
			},
			last : {
				iconCls : "pagination-last",
				handler : function() {
					var opts = $(this).pagination("options");
					var pageNumber = Math.ceil(opts.total / opts.pageSize);
					if (opts.pageNumber < pageNumber) {
						$(this).pagination("select", pageNumber);
					}
				}
			},
			refresh : {
				iconCls : "pagination-refresh",
				handler : function() {
					var opts = $(this).pagination("options");
					if (opts.onBeforeRefresh.call(this, opts.pageNumber, opts.pageSize) != false) {
						$(this).pagination("select", opts.pageNumber);
						opts.onRefresh.call(this, opts.pageNumber, opts.pageSize);
					}
				}
			}
		}
	};
})(jQuery);
